let text = $('#md-content').attr('value');

let converter = new showdown.Converter({
    tables: true,
    moreStyling: true,
    openLinksInNewWindow: true
});
let html = converter.makeHtml(text);
$('#article-content').html(html);

//article like
function like() {
    //dont login
    if (!isLogin()) {
        alert("请先登录");
        return;
    }
    let obj = $('#like-ops');
    let isLike = obj.attr('islike');
    let articleId = $('#value-article-id').attr('value');
    let likeVal = parseInt($("#value-like-count").text());
    if (isLike == 'true') {
        //后台减少点赞数
        $.ajax({
            url: '/articleOpe/unlike',
            method: 'POST',
            headers: {
                Authorization: localStorage.getItem("jwt-token")
            },
            data: {
                articleId: articleId
            },
            success: function (res) {
                if (res.result == true) {
                    obj.attr('src', '/img/like-icon.png');
                    obj.attr('islike', 'false');
                    likeVal--;
                    $("#value-like-count").text(likeVal);
                    setTimeout(function () {
                        alert("取消点赞成功");
                    }, 30);
                } else {
                    badToken();
                }

            }
        });
    } else if (isLike == 'false') {
        //后台增加文章点赞数
        $.ajax({
            url: '/articleOpe/like',
            method: 'POST',
            headers: {
                Authorization: localStorage.getItem("jwt-token")
            },
            data: {
                articleId: articleId
            },
            success: function (res) {
                if (res.result == true) {
                    obj.attr('src', '/img/like-icon-active.png');
                    obj.attr('islike', 'true');
                    likeVal++;
                    $("#value-like-count").text(likeVal);
                    setTimeout(function () {
                        alert("点赞成功,感谢对作者的支持");
                    }, 30);
                } else {
                    badToken();
                }
            }
        });
    }
}

//comment like
function likeComment(e) {
    if (!isLogin()) {
        alert("请先登录");
        return;
    }
    //点赞图标id
    let btn_id = e.id;
    //object
    let obj = $('#' + btn_id);
    //获取点赞的评论id
    let commentId = btn_id.substring(btn_id.lastIndexOf('_') + 1);
    //获取点赞状态
    let isLike = obj.attr("islike");
    //获取点赞数
    let countObj = $('#value-comment-like_' + commentId);
    let likeVal = parseInt(countObj.text());

    if (isLike == 'false') {
        $.ajax({
            url: '/articleOpe/likeComment',
            method: 'POST',
            headers: {
                Authorization: localStorage.getItem("jwt-token")
            },
            data: {
                commentId: commentId
            },
            success: function (res) {
                if (res.result == true) {
                    obj.attr('src', '/img/like-icon-active.png');
                    obj.attr('islike', 'true');
                    likeVal++;
                    countObj.text(likeVal);
                } else {
                    badToken();
                }

            }
        });
    } else {
        $.ajax({
            url: '/articleOpe/unlikeComment',
            method: 'POST',
            headers: {
                Authorization: localStorage.getItem("jwt-token")
            },
            data: {
                commentId: commentId
            },
            success: function (res) {
                if (res.result == true) {
                    obj.attr('src', '/img/like-icon.png');
                    obj.attr('islike', 'false');
                    likeVal--;
                    countObj.text(likeVal);
                } else {
                    badToken();
                }
            }
        });
    }


}

//comment
function publishComment() {
    //dont login
    if (!isLogin()) {
        alert("请先登录");
        return;
    }
    let articleId = $('#value-article-id').attr('value');
    let content = $('#val-comment-content').val();
    //empty content
    if (content == '') {
        alert("评论不能为空");
        return;
    }

    $.ajax({
        url: '/articleOpe/comment',
        method: 'POST',
        headers: {
            Authorization: localStorage.getItem("jwt-token")
        },
        data: {
            content: content,
            articleId: articleId
        },
        success: function (res) {
            if (res.result == true) {
                window.location = window.location + '#val-comment-content';
                window.location.reload();
            } else {
                badToken();
            }

        }
    });

}

//second comment
function publishSecondComment(e) {
    //dont login
    if (!isLogin()) {
        alert("请先登录");
        return;
    }
    //btn id
    let btn_id = e.id;
    //获取一级评论i（父评论）id
    let commentId = btn_id.substring(btn_id.lastIndexOf('_') + 1);
    //获取评论文章id
    let articleId = $('#value-article-id').attr('value');
    //获取二级评论内容
    let inputElement = '#second-comment-content_' + commentId;
    let content = $(inputElement).val();
    //empty content
    if (content == '') {
        alert("评论不能为空");
        return;
    }

    $.ajax({
        url: '/articleOpe/secondComment',
        method: 'POST',
        headers: {
            Authorization: localStorage.getItem("jwt-token")
        },
        data: {
            content: content,
            articleId: articleId,
            commentId: commentId
        },
        success: function (res) {
            if (res.result == true) {
                window.location = window.location + inputElement;
                window.location.reload();
            } else {
                badToken();
            }

        }
    });
}

//判断登录状态
function isLogin() {
    let isLike = $('#like-ops').attr('islike');
    return !(isLike == 'unlogin');
}

//token验证失败，提示重新登录
function badToken() {
    alert("身份过期，请重新登录");
    window.location.reload();
}